package Application;

import Application.Controller.MainWindowController;
import Application.File.FileAdd;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import Application.LayerImage.LayerImage;
import javafx.scene.image.ImageView;
import javafx.scene.control.ComboBox;
import Application.Tool.Tool;
import java.io.File;
import java.util.ArrayList;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;

/**
 * 数据中枢
 * 数据处理中枢
 */
public final class AppJoint {


    //是否已经存在renamebox

    public boolean isRenameBoxActivited() {
        return renameBoxActivited;
    }

    public void setRenameBoxActivited(boolean renameBoxActivited) {
        this.renameBoxActivited = renameBoxActivited;
    }

    boolean renameBoxActivited = false;

    //是否执行了开始界面
    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    private boolean start = false;

    //是否保存了
    public boolean isSAVED() {
        return SAVED;
    }

    public void setSAVED(boolean SAVED) {
        this.SAVED = SAVED;
    }

    private boolean SAVED = false;

    //前景色argb

    public int getFrontColorArgb() {
        return frontColorArgb;
    }

    public void setFrontColorArgb(int frontColorArgb) {
        this.frontColorArgb = frontColorArgb;
    }

    public int getBackColorArgb() {
        return backColorArgb;
    }

    public void setBackColorArgb(int backColorArgb) {
        this.backColorArgb = backColorArgb;
    }

    private int frontColorArgb=(0xffffffff);

    //后景色argb

    private int backColorArgb=(0xffffffff);


    private static AppJoint appJoint = new AppJoint();

    // 画布宽度
    private int CANVAS_WIDTH = 400;
    // 画布高度
    private int CANVAS_HEIGHT = 400;
    // 当前被选中的图层Id
    private int activeLayerId = -1;
    // 最终的图片，需要设置完工程画布大小之后再创建
    LayerImage outImg = null;
    // 当前选中的工具
    Tool activeTool = null;

    public static Color selectedColor = Color.rgb(80, 80, 80);
    public static Color unselectedColor = Color.rgb(60, 63, 65);

    ImageView view = null;

    private AppJoint() { }

    public static AppJoint getInstance() {
        return appJoint;
    }

    public void setCanvasHeight(int height) {
        this.CANVAS_HEIGHT = height;
    }

    public int getCanvasHeight() {
        return this.CANVAS_HEIGHT;
    }

    public void setCanvasWidth(int width) {
        this.CANVAS_WIDTH = width;
    }

    public int getCanvasWidth() {
        return this.CANVAS_WIDTH;
    }

    public int getActiveLayerId() {
        return activeLayerId;
    }

    public void setActiveLayerId(int id) {
        this.activeLayerId = id;
        System.out.println(id);
    }



    // 该函数需要在设置完画布宽高后执行
    public void createOutImage() {
        outImg = new LayerImage(CANVAS_WIDTH, CANVAS_HEIGHT);
    }

    public void setOutImg(LayerImage layerImage){
        this.outImg = layerImage;
    }
    public LayerImage getOutImg() {
        return outImg;
    }

    public void setImageView(ImageView view) {
        this.view = view;
    }

    public ImageView getImageView() {
        return this.view;
    }

    // 将更新后的outputImg显示到view上
    public void flush() {
        view.setImage(outImg.getWritableImage());
        //System.out.println("aaa" + outImg.getPixelColor(1, 1));
    }

    public void bondImg(){
        view.setImage(outImg.getWritableImage());
    }
    public void setActiveTool(Tool tool) {
        this.activeTool = tool;
    }

    public Tool getActiveTool() {
        return activeTool;
    }

    //mainWindowController

    public MainWindowController getMainWindowController() {
        return mainWindowController;
    }

    public void setMainWindowController(MainWindowController mainWindowController) {
        this.mainWindowController = mainWindowController;
    }

    private MainWindowController mainWindowController;

    //新建图层时的清除操作
    public void clearAll(){
        for(int i=0;i<outImg.getWidth();i++){
            for(int j=0;j<outImg.getHeight();j++){
                outImg.setPixelArgb(i,j,(0x00)<<24);
            }
        }

        setSAVED(false);
        setActiveLayerId(-1);
        setOutImg(null);

        VBox vbox = getMainWindowController().getVboxLayer();
        int length = vbox.getChildren().size();
        for(int i=length-1;i>=0;i--){
            vbox.getChildren().remove(i);
        }

        ArrayList<Layer> layers = LayerDelegate.getInstance().getLayers();
        length = layers.size();

        for(int i=length-1;i>=0;i--){
            layers.remove(i);
        }

        Layer.globalId=0;
        //LayerDelegate.getInstance().refreshAll();
        //flush();
    }

//    public void loadMasksToComboBox(ComboBox<String> combo) {
//        combo.getItems().addAll(
//                "test",
//                "test2",
//                "test4"
//        );
//    }

    public void loadMasksToComboBox(ComboBox<String> combo) {
        File[] masks = FileAdd.getFilesByPath("Assets/masks/");

        for(File file : masks) {
            String name = file.getName();
            combo.getItems().add(name.substring(0, name.length() - 4));
        }
        combo.setCellFactory(listview -> new StringImageCell());
        combo.setButtonCell(new StringImageCell());
    }

    public void loadBlendModeToComboBox(ComboBox<String> combo) {
        combo.getItems().addAll(
                "正常",
                "正片叠底"
        );
    }

    static class StringImageCell extends ListCell<String> {
        Label label;

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
//            if(item == null || empty) {
//                System.out.println("aaa");
//                setItem(null);
//                setGraphic(null);
//            } else {
                setText(item);
                ImageView image = new ImageView(new Image("file:Assets/masks/" + item + ".png", 24, 24, true, false));
                label = new Label("", image);
                //System.out.println(label);
                setGraphic(label);
            //}
        }
    }
}
